<template>
  <div  id="index">
    <el-container>
      <el-dialog title="修改姓名" :visible.sync="dialogFormVisible" style="position: absolute;width: 50%;
    left: 25pc;">
        <el-form>
          <el-form-item label="姓名">
            <el-input v-model="username" autocomplete="off" style="width: 22pc;
    float: left;"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary"  @click="updateName">确 定</el-button>
        </div>
      </el-dialog>
      <el-header>
        <el-row>
          <el-col :span="0.6">
            <img src="../assets/logo-yifan-houtai.png" style="width: 30px;
    margin-top: 14px;padding-right: 6px;margin-left: 16px;"/>
          </el-col>
          <el-col :span="3">
            高校就业管理分析平台
          </el-col>
          <el-col :span="5">
          </el-col>
          <el-col :span="19" style="text-align: right;
                                    padding-right: 22px;"
                      v-if="this.$store.state.username != ''">
            欢迎您&nbsp;&nbsp;{{this.$store.state.username}}</el-col>
            <el-col :span="19" style="text-align: right;
                                    padding-right: 22px;"
                      v-else>
            未登录，请<router-link to="/">登录</router-link></el-col>
          <el-col :span="0.5">
            <el-dropdown>
              <span class="el-dropdown-link">
                <i class="el-icon-s-tools"></i>
              </span>
              <el-dropdown-menu slot="dropdown" v-if="this.$store.state.username != ''">
                <el-dropdown-item class="clearfix" >
                  <p @click="dialogFormVisible = true">修改名字</p>
                </el-dropdown-item>
                <el-dropdown-item class="clearfix"  >
                  
                  <p @click="exit">退出</p>
                </el-dropdown-item>
              </el-dropdown-menu>
              <!-- <el-dropdown-menu slot="dropdown">
                <button type="text" style="border: none; background: none;" @click="dialogFormVisible = true"><el-dropdown-item>修改名字</el-dropdown-item></button>
                <button type="text" style="border: none; background: none;" @click="exit"><el-dropdown-item>退出</el-dropdown-item></button>
              </el-dropdown-menu> -->
            </el-dropdown>
          </el-col>
        </el-row>
      </el-header>
      <el-container style=" display: flex;
                            flex: 1">
        <el-aside width="201px">
          <el-menu
            default-active="2"
            class="el-menu-vertical-demo"
            @open="handleOpen"
            @close="handleClose"
            background-color="#526384"
            text-color="#fff"
            :router='true'
            active-text-color="#409EFF">
            <el-menu-item index="/home">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>主页</span>
              </template>
            </el-menu-item>
            <el-submenu index="2">
              <template slot="title">
                <i class="el-icon-coin"></i>
                <span>数据可视化</span>
              </template>
              <el-menu-item index="collegevisualization">学院可视化</el-menu-item>
              <el-menu-item index="2-2">选项1</el-menu-item>
              <el-menu-item index="2-3">选项1</el-menu-item>
              <el-menu-item index="2-4">选项1</el-menu-item>
            </el-submenu>
            <el-menu-item index="3" disabled>
              <i class="el-icon-document"></i>
              <span slot="title">导航三</span>
            </el-menu-item>
            <el-submenu index="4">
              <template slot="title">
                <i class="el-icon-user-solid"></i>
                <span>个人中心</span>
              </template>
              <el-menu-item index="userDetail">个人信息</el-menu-item>
              <el-menu-item index="userEmploye">就业信息</el-menu-item>
            </el-submenu>
          </el-menu>

        </el-aside>
        <el-main>
          <el-row style="background: white;">
          <el-tabs v-model="editableTabsValue" type="card" closable @tab-remove="removeTab" style="margin-bottom: -14px;">
            <el-tab-pane
              v-for="item in this.$store.state.editableTabs"
              :key="item.index"
              :label="item.title"
              :name="item.route"
            >
            </el-tab-pane>
            <el-tab-pane
              label="关闭所有"
              name="close"
            ></el-tab-pane>
          </el-tabs>
          </el-row>
          <el-row>
            <div><router-view></router-view></div>
          </el-row>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name:'',
  data () {
    return {
        dialogFormVisible: false,
        username: '',
        isCollapse: true,
        editableTabsValue: 'close',
        editableTabs: [],
        tabIndex: 0
    }
  },
  watch: {
    editableTabsValue (value) {
      console.log(value);
      if (this.$router.currentRoute.path != value && value != '') {
        if (value != 'close') {
          this.$router.push(value)
        } else {
          if (this.$router.currentRoute.path != '/index')
            this.$router.replace('/index')

          let that = this
          setTimeout(() => {
            that.$store.state.editableTabs = []
            that.editableTabsValue = '';
          }, 100);
        }
      }
    },
  },
  created() {
    this.util.post('/user/test').then(res => {
      console.log(res);
    })
    this.$store.state.username =  localStorage.getItem('username')
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    updateName () {
      let formdata = new FormData();
      formdata.append('oldUsername', this.$store.state.username);
      formdata.append('newUsername', this.username);
      this.dialogFormVisible = false
      axios({
        method: 'post',
        url: 'http://localhost:8000/public/updateNameByName',
        data: formdata
      }).then(res => {
        localStorage.setItem('username', this.username)
        this.$store.state.username =  this.username
        this.username = ''
      })
    },
    exit () {
      this.util.post('/logout').then(res => {
        if (res.data == 'loginPage')
          this.$router.push('/')
        this.$store.state.username = ''
         localStorage.setItem('username', '')
      })
    },
    addTab(targetName) {
        let newTabName = ++this.tabIndex + '';
        this.editableTabs.push({
          title: 'New Tab',
          name: newTabName,
          content: 'New Tab content'
        });
        this.editableTabsValue = newTabName;
      },
      removeTab(targetName) {
        this.editableTabsValue = '/index';
        this.$store.state.editableTabs = this.$store.state.editableTabs.filter(tab => tab.route !== targetName);
      }
  },
  
}

</script>
<style lang='' scoped>
/* #index {
  background-image: url(../assets/index.jpg);
  background-size: 100%;
  background-repeat: no-repeat;
  background-position: center center;
} */
.el-header, .el-footer {
    background-color: #445674;
    color: white;
    text-align: left;
    height: 6pc;
    line-height: 3.5pc;
  }
  
  .el-aside {
    color: #333;
    text-align: left;
    display: flex;
    min-height: 92.5vh;
  }
  
  .el-main {
    background-color: rgb(229,236,247);
    color: #333;
    text-align: center;
    padding: 0;
  }
  
  body > .el-container {
    margin-bottom: 40px;
  }
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }

  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
</style>
